package com.wanggsx.library.util.fun.media.player;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Handler;

import java.util.HashMap;
import java.util.Map;

public class SoundPoolUtil {

    public static SoundPool soundPool;
    public static MediaPlayer mediaPlayer;
    private static int stopRes;
    private static Context _context;
    private static Map<Integer, Integer> mSoundKeyOfMap;
    //资源raw Id,播放id
    public static HashMap<Integer, Integer> mSoundPoolMap = new HashMap<Integer, Integer>();

    /**
     * 播放音效
     *
     * @param context
     * @param soundRes 音效资源文件
     * @param number   循环次数，0是不循环，-1是永远循环
     */
    public static void playSound(Context context, final int soundRes,
                                 final int number) {
        _context = context;

        loadVoice(context, soundRes);

        AudioManager am = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        // 最大音量
        float audioMaxVolumn = am
                .getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
        // 当前音量
        float audioCurrentVolumn = am
                .getStreamVolume(AudioManager.STREAM_SYSTEM);
        final float volumnRatio = audioCurrentVolumn * 1f / audioMaxVolumn * 0.5f;

        // 獲取當前媒體音量
        int voiceMedia = am.getStreamVolume(AudioManager.STREAM_MUSIC);

        // 如果系統音量為0則不播放音效
        if ((int) audioCurrentVolumn == 0 || voiceMedia == 0) {
            return;
        }

        // 播放
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                stopRes = soundPool.play(getVoiceRes(soundRes), // 声音资源
                        volumnRatio, // 左声道
                        volumnRatio, // 右声道
                        1, // 优先级，0最低
                        number, // 循环次数，0是不循环，-1是永远循环
                        1); // 回放速度，0.5-2.0之间。1为正常速度

            }
        }, 100);

    }

    /**
     * 其中声音文件放到raw目录下面
     *
     * @param context  Context对象
     * @param soundRes raw文件Id
     */
    public static void playMedia(Context context, final int soundRes) {

        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
        mediaPlayer = MediaPlayer.create(context,
                soundRes);

        AudioManager am = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        // 最大音量
        float audioMaxVolumn = am
                .getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
        // 当前音量
        float audioCurrentVolumn = am
                .getStreamVolume(AudioManager.STREAM_SYSTEM);
        final float volumnRatio = audioCurrentVolumn * 1f / audioMaxVolumn * 0.5f;
        mediaPlayer.setVolume(volumnRatio, audioMaxVolumn);
        // 播放
        mediaPlayer.start();
    }


    /**
     * 初始化声音文件加载
     *
     * @param context
     */
    public static void loadVoice(Context context,
                                 int soundPoolKeyRawIdMap) {
        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
        if (mSoundKeyOfMap == null) {
            mSoundKeyOfMap = new HashMap<Integer, Integer>();
        }
        if (!mSoundKeyOfMap.containsKey(soundPoolKeyRawIdMap)) {
            mSoundPoolMap.put(soundPoolKeyRawIdMap, soundPool.load(context, soundPoolKeyRawIdMap, 1));
        }
    }

    /**
     * 获取音效
     *
     * @param Res
     * @return
     */
    private static int getVoiceRes(int Res) {
        return mSoundPoolMap.get(Res);
    }

    /**
     * 暂停播放
     *
     * @param
     */
    public static void pauseSoundPool() {
        try {
            soundPool.stop(stopRes);
            soundPool.release();
            loadVoice(_context, 0);
            if (mediaPlayer != null) {
                mediaPlayer.pause();
            }
        } catch (Exception e) {

        }
    }

}
